# promethus_install script
#需要提前准备promethues二进制安装包，需要准备的二进制安装包有:promethues,altermanager,node_exporter,consul_export
#所有软件安装包下载地址：https://prometheus.io/download/
#脚本提供的基于consul单点的服务于发现，所有服务在一台服务器上进行安装与配置。
#consul 下载地址： https://www.consul.io/downloads

#!/bin/bash
function consul_install(){
	##consul监听在8300端口
	cd /software/ && unzip consul_1.9.3_linux_amd64.zip -d /usr/local/sbin/
	mkdir /etc/consul.d -p
	mkdir /data/consul -p 

# 配置consul的服务于发现
cat >/etc/consul.d/node.json <<EOF
{
    "services":[
        {
            "id": "node_exporter-node01",
            "name": "node01",
            "address": "192.168.10.200",
            "port": 9100,
            "tags": ["nodes"],
            "checks": [{
                "http": "http://192.168.10.200:9100/metrics",
                "interval": "5s"
            }]
        },
        {
            "id": "node_exporter-node02",
            "name": "node02",
            "address": "192.168.10.160",
            "port": 9100,
            "tags": ["nodes"],
            "checks": [{
                "http": "http://192.168.10.160:9100/metrics",
                "interval": "5s"
            }]
        }
    ]
}
EOF

cat >/etc/consul.d/promethues.json <<EOF
{
    "service":[
        {
            "id": "promethues",
            "name": "promethues",
            "address": "192.168.10.200",
            "port": 9090,
            "tags": ["promethues"],
            "checks": [{
                "http": "http://192.168.10.200:9090/metrics",
                "interval": "5s"
            }]
        }
    ]
}
EOF

cat >/etc/consul.d/consul.json <<EOF
{
    "service":[
        {
            "id": "consul",
            "name": "consul",
            "address": "192.168.10.200",
            "port": 9107,
            "tags": ["consul"],
            "checks": [{
                "http": "http://192.168.10.200:9093/metrics",
                "interval": "5s"
            }]
        }
    ]
}
EOF
# 启动consul
nohup consul agent -dev -ui -data-dir=/data/consul/  -config-dir=/etc/consul.d/ -client=0.0.0.0 &

# consual 新新添加配置文件后使用consul reload 从新加载配置文件
}

function prometheus_install(){
	tar -xf prometheus-2.25.0.linux-amd64.tar.gz -C /usr/local/
	ln -sv prometheus-2.25.0.linux-amd64 prometheus
  cp /usr/local/promethues/promethues /usr/local/sbin/
	cd /usr/local/prometheus/ && mkdir consul-sd
## 配置promethues
cat >/usr/loca/prometheus/consul-sd/prometheus.yml << EOF
# my global config
global:
  scrape_interval:     15s # Set the scrape interval to every 15 seconds. Default is every 1 minute.
  evaluation_interval: 15s # Evaluate rules every 15 seconds. The default is every 1 minute.
  # scrape_timeout is set to the global default (10s).

# Alertmanager configuration
alerting:
  alertmanagers:
  - static_configs:
    - targets:
      # - alertmanager:9093

# Load rules once and periodically evaluate them according to the global 'evaluation_interval'.
rule_files:
  # - "first_rules.yml"
  # - "second_rules.yml"

# A scrape configuration containing exactly one endpoint to scrape:
# Here it's Prometheus itself.
scrape_configs:
#node_export
  - job_name: 'nodes'
    consul_sd_configs:
    - server: "192.168.10.200:8500"
      tags:
      - "nodes"
      refresh_interval: 2m
#promethues
  - job_name: 'promethues'
    consul_sd_configs:
    - server: "192.168.10.200:8500"
      tags:
      - "promethues"
      refresh_interval: 2m
# alertmannger
  - job_name: 'alertmannager'
    consul_sd_configs:
    - server: "192.168.10.200:8500"
      tags:
      - "alertmannager"
      refresh_interval: 2m
#consul
  - job_name: 'consul'
    consul_sd_configs:
    - server: "192.168.10.200:8500"
    refresh_interval: 2m
EOF

## 启动 promethues
nohup promethues -config.file=/usr/local/promethues/consul-sd/promethues.yml &
## prome重新加载配置文件 kill -HUP [promethues_pid]
}

# alertmannager 监听在9093端口
function alertmannager_install(){
   tar -xf  -C alertmanager-0.21.0.linux-amd64.tar.gz /usr/local/
   ln -sv alertmanager-0.21.0.linux-amd64 alertmannager
   cp /usr/local/alertmannager/alertmannager /usr/local/sbin/
   nohup alertmannager & 
}

# node_exporter 监听在9100 端口
function node_exporter_install(){
  tar -xf node_exporter-1.1.1.linux-amd64 -C /usr/local/sbin/
  nohup node_exporter &
}


# consul_exporter 监听在9107端口
function consul_exporter_install{
   tar -xf  consul_exporter-0.7.1.linux-amd64.tar.gz  /usr/local/sbin/
   nohup consul_exporter &
}

consul_install
prometheus_install
alertmannager_install
node_exporter_install
consul_exporter_install